package org.yxj.videolibrary.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import org.yxj.videolibrary.R;

/**
 * 视频加载view
 *
 * @author on 2022/9/12
 */
public class VideoLoadView extends FrameLayout {


    /**
     * 动画模式
     */
    public enum LoadAnimMode{
        FRAME_ANIM,//帧动画
        ATTRIBUTE_ANIM ; //属性动画
    }


    private View view ;

    private ProgressBar progressBar ;

    /**
     * 默认帧动画
     */
    private LoadAnimMode mode = LoadAnimMode.ATTRIBUTE_ANIM ;


    public VideoLoadView( Context context) {
        this(context,null);
    }


    public VideoLoadView( Context context,  AttributeSet attrs) {
        this(context, attrs,0);
    }


    public VideoLoadView( Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        addView(view = LayoutInflater.from(context).inflate(R.layout.video_loading_layout,this,false));
        progressBar = view.findViewById(R.id.progressBar);
        setDefaultLoading(mode);
    }



    /**
     *
     * @param mode
     */
    public void setDefaultLoading(LoadAnimMode mode){
        if (mode == null)return;
        switch (mode){
            case FRAME_ANIM:
                measureLoadView(80,40);
                progressBar.setIndeterminateDrawable(getResources().getDrawable(R.drawable.loading));
                break;
            case ATTRIBUTE_ANIM:
                measureLoadView(80,80);
                progressBar.setIndeterminateDrawable(getResources().getDrawable(R.drawable.jz_loading));
                break;
        }
    }




    /**
     * 设置加载动画
     *
     * 内置两个加载动画
     */
    public void setLoadDrawable(Drawable drawable){
        if (drawable!=null){
            progressBar.setIndeterminateDrawable(drawable);
        }
    }



    /**
     *
     * @param w
     * @param h
     */
    private void measureLoadView(int w,int h) {
        ViewGroup.LayoutParams p = progressBar.getLayoutParams();;
        p.width = w ;
        p.height = h ;
        progressBar.setLayoutParams(p);
    }
}
